package com.xn.app.muying.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.HosptialInfoListBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/11.
 */
public class HosptialInfoAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<HosptialInfoListBean> list = new ArrayList<>();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public HosptialInfoAdapter(Context context, ImageLoader loader,
                               DisplayImageOptions options) {
        this.context = context;
        this.imageLoader = loader;
        this.options = options;
        inflater = LayoutInflater.from(context);
    }

    public void getData(ArrayList<HosptialInfoListBean> data) {
        this.list=data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.activity_hosptial_item, null);
            holder = new ViewHolder();
            holder.time_tv = (TextView) view.findViewById(R.id.activity_hosptial_item_time);
            holder.title_tv = (TextView) view.findViewById(R.id.activity_hosptial_item_title);
            holder.picture_iv = (ImageView) view.findViewById(R.id.activity_hosptial_item_picture);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String[] strs = list.get(i).getCreationTime().split("T");
        String[] strs2 = strs[1].split(":");
        holder.time_tv.setText(strs[0] + " " + strs2[0] + ":" + strs2[1]);
        holder.title_tv.setText(list.get(i).getTitle());
        holder.picture_iv.setTag(list.get(i).getPictureUrl());
        //设置图片，如果内存中有，优先从内存中读取
        if (list.get(i).getPictureUrl() != null) {
            imageLoader.displayImage(list.get(i).getPictureUrl(), holder.picture_iv, options);
        }
        return view;
    }

    class ViewHolder {
        TextView time_tv, title_tv;
        ImageView picture_iv;
    }
}
